#include "usart.h"
#include "RS485.h"
#include <string.h>//memset
#include "tim.H"

// #include "app_FIFO.h"


static u16 const BUF_SIZE=512;
static uint8_t g_TxBuf[BUF_SIZE];		/* 发送缓冲区 */
static uint8_t g_RxBuf[BUF_SIZE];		/* 接收缓冲区 */

//---------------------------------------------------------------------------------------------------
HAL_StatusTypeDef RS485_DataErr();


// static int GetData_State(RS485_private* _usart);
// static void CLRData_State(RS485_private* _usart);

//------------------------------------------------------------------------


void RS485_Receive(u8* data)
{
	RS485_2_UART_RX;
	HAL_UART_Receive_IT(&huart3 ,data,1);
	
}
void RS485_Transmit(u8*  data)
{
	RS485_2_UART_TX;
	HAL_UART_Transmit_IT(&huart3 ,(u8*)data,1);


}
void RS485_SendBuf(u8*  buf,u16 buf_len)
{
	RS485_2_UART_TX;

	HAL_UART_Transmit(&huart3 ,buf,buf_len,10);
	// HAL_UART_Transmit_DMA(&huart3 ,buf,buf_len);

	// HAL_UART_Transmit_IT(&huart3 ,buf,buf_len);
	// while(!__HAL_UART_GET_FLAG(&huart3,UART_FLAG_TC));
	RS485_2_UART_RX;


}

static void RS485_uartRX_CallBack(USART_TypeDef uart)
{

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{

    if(huart->Instance==USART1)
    {
		// RS485_uartRX_CallBack();
    
    }
	else if(huart->Instance==USART2)
	{}
	else if(huart->Instance==USART3)
    {
		// HAL_UART_DMAStop(&huart3);

    }
}

// static int GetData_State(RS485_private* _usart)
// {
//     return (GetBit(_usart->flag,Bit1));
// }
// static void CLRData_State(RS485_private* _usart)
// {
// 	ClrBit(_usart->flag,Bit1);
// }


void Init_RS485()
{

	RS485_1_TX();
	RS485_2_UART_RX;

}


void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
 {

    if(huart->Instance==USART2)
    {
            
        u32 checkFlag=0;
        // checkFlag= HAL_UART_GetError(huart);
        checkFlag= huart->ErrorCode;
        if(checkFlag)
        {
            //帧错误标志
            if(HAL_UART_ERROR_FE ==checkFlag)
            {
                __HAL_UART_CLEAR_FEFLAG(huart);
            
            }
            
            //溢出错误
            if(HAL_UART_ERROR_ORE ==checkFlag)
            {    

                __HAL_UART_CLEAR_OREFLAG(huart);

            }
        }
    }
    if(huart->Instance==USART3)
    {
            
        u32 checkFlag=0;
        // checkFlag= HAL_UART_GetError(huart);
        checkFlag= huart->ErrorCode;
        if(checkFlag)
        {
            //帧错误标志
            if(HAL_UART_ERROR_FE ==checkFlag)
            {
                __HAL_UART_CLEAR_FEFLAG(huart);
            
            }
            
            //溢出错误
            if(HAL_UART_ERROR_ORE ==checkFlag)
            {    

                __HAL_UART_CLEAR_OREFLAG(huart);

            }
        }
    }
 }

//空闲中断回调函数
void USER_UART_IDLECallback(UART_HandleTypeDef *huart)
{
     if(huart->Instance==USART2)
    {

    }

}

//-----------------------------------------------------------------------



static void RS485_TX_CallBack(UART_HandleTypeDef *huart)
{

	

}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance==USART1)
	{

		// Lamp_send_TxCpltCallback();
	}
    else if(huart->Instance==USART2)
	{
	}
    else if(huart->Instance==USART3)
    {
	


    }
	

	
}

//--------------------------------------------------------------------------------------------------


#if(0)
#include<stdio.h>
int fputc(int ch,FILE *f)
{
	RS485_2_UART_TX;

	HAL_UART_Transmit(&huart3,(uint8_t *)&ch,1,0xff);
	// HAL_UART_Transmit_IT(&huart3,(uint8_t *)&ch,1);
	//  HAL_UART_Transmit_DMA(&huart3,(u8*)ch,1);
	return ch;
}

#endif
